//
//  UIView+RBZoom.m
//  RBDemoProj
//
//  Created by lanchengyu on 2019/8/16.
//  Copyright © 2019 SummerTea. All rights reserved.
//

#import "UIView+RBZoom.h"
#import "NSObject+YYAdd.h"
#import "RBDefine.h"
static char *const RB_ZOOM_CLOSE_KEY = "RB_ZOOM_COLSE";

@implementation UIView(RBZoom)

- (void)setZoomClose:(BOOL)zoomClose
{
    if (zoomClose)
    {
        [self setAssociateValue:@"1" withKey:RB_ZOOM_CLOSE_KEY];
    }
    else
    {
        [self setAssociateValue:nil withKey:RB_ZOOM_CLOSE_KEY];
    }
}

- (BOOL)zoomClose
{
    return [self getAssociatedValueForKey:RB_ZOOM_CLOSE_KEY] != nil;
}

- (void)zoomSubViews
{
    [self zoomSubViewsWithView:self ignoreView:self];
}

- (void)zoomSubViewsWithSelf
{
    [self zoomSubViewsWithView:self ignoreView:nil];
}

- (void)zoomSubViewsWithView: (UIView *)superView ignoreView: (UIView *)ignoreView
{
    //开关屏蔽
    if (superView.zoomClose)
    {
        return;
    }
    
    //父view缩放
    if (superView != ignoreView)
    {
        [self zoomView:superView];
    }
    
    //类屏蔽 子view
    if (
        [superView isKindOfClass:[UIButton class]] ||
        [superView isKindOfClass:[UITextField class]] ||
        [superView isKindOfClass:[UITextView class]] ||
        [superView isKindOfClass:[UITableView class]] ||
        [superView isKindOfClass:[UICollectionView class]] ||
        [superView isKindOfClass:[UISwitch class]]
        )
    {
        return;
    }
    
    //子view缩放
    if ([superView isKindOfClass:[UIStackView class]])
    {
        for (UIView *subView in ((UIStackView *)superView).arrangedSubviews)
        {
            [self zoomSubViewsWithView:subView ignoreView:nil];
        }
    }
    else
    {
        for (UIView *subView in superView.subviews)
        {
            [self zoomSubViewsWithView:subView ignoreView:nil];
        }
    }
}

- (void)zoomView: (UIView *)view
{
    CGFloat factor = kRBFactor;
    //约束
    if ([view isKindOfClass:[UISwitch class]])
    {
        for (NSLayoutConstraint *layoutConstraint in view.constraints)
        {
            if (layoutConstraint.firstAttribute == NSLayoutAttributeWidth || layoutConstraint.firstAttribute == NSLayoutAttributeHeight ||
                layoutConstraint.secondAttribute == NSLayoutAttributeWidth ||
                layoutConstraint.secondAttribute == NSLayoutAttributeHeight)
            {
                
            }
            else
            {
                layoutConstraint.constant *= factor;
            }
        }
        return;
    }
    else
    {
        for (NSLayoutConstraint *layoutConstraint in view.constraints)
        {
            layoutConstraint.constant *= factor;
        }
    }
    //属性
    if ([view isKindOfClass:[UIButton class]])
    {
        UIButton *item = (UIButton *)view;
        item.titleLabel.font = [item.titleLabel.font fontWithSize:item.titleLabel.font.pointSize*factor];
    }
    else if ([view isKindOfClass:[UILabel class]])
    {
        UILabel *item = (UILabel *)view;
        item.font = [item.font fontWithSize:item.font.pointSize*factor];
    }
    else if ([view isKindOfClass:[UITextField class]])
    {
        UITextField *item = (UITextField *)view;
        item.font = [item.font fontWithSize:item.font.pointSize*factor];
    }
    else if ([view isKindOfClass:[UITextView class]])
    {
        UITextView *item = (UITextView *)view;
        item.font = [item.font fontWithSize:item.font.pointSize*factor];
    }
    else if ([view isKindOfClass:[UIStackView class]])
    {
        ((UIStackView *)view).spacing *= factor;
    }
    //圆角 外框
    view.layer.cornerRadius *= factor;
    view.layer.borderWidth *= factor;
}

@end
